//
//  USPhotoCell.m
//  us
//
//  Created by karry on 2025/4/11.
//

#import "USPhotoCell.h"

@interface USPhotoCell ()

@property (nonatomic,strong)UIImageView *imageView;

@property (nonatomic, strong)UIView *liveTagView;

@property (nonatomic, strong)UIImageView *videoImageView;

@property (nonatomic, strong) UIImageView *selectImageView;
@end

@implementation USPhotoCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
 
    NSString *imageName = selected ? @"photo_selected_1" : @"photo_selected_bg_normal";
    self.selectImageView.image = [UIImage imageNamed:imageName];
    
}

- (void)setEdit:(BOOL)edit{
    _edit = edit;
    
    self.selectImageView.hidden = !edit;
 
}

- (void)setPhotoModel:(USPhotoModel *)photoModel {
    _photoModel = photoModel;
    
    if (photoModel.type == USMediaTypeImage) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoModel.cosUrl.photoThumbnail]];
    }else if (photoModel.type == USMediaTypeVideo) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoModel.cosUrl.videoThumbnail]];
    }else if (photoModel.type == USMediaTypeLivePhoto) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoModel.cosUrl.photoThumbnail]];
    }
    
    self.liveTagView.hidden = photoModel.type != USMediaTypeLivePhoto;
    self.videoImageView.hidden = photoModel.type != USMediaTypeVideo;

}
 

- (void)setupView{
    
    self.imageView = [[UIImageView alloc]initWithImage:nil];
    self.imageView.backgroundColor = [[UIColor jk_randomMacaronColor] colorWithAlphaComponent:.5];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
    }];
    self.contentView.clipsToBounds = YES;
 
    
    // 添加 live 标签
    [self.contentView addSubview:self.liveTagView];
    [self.liveTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(4);
        make.bottom.mas_equalTo(-4);
        make.height.mas_equalTo(20);
    }];
    
    self.selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo_selected_bg_normal"]];
    self.selectImageView.hidden = YES;
    [self.contentView addSubview:self.selectImageView];
    [self.selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(6);
        make.right.mas_equalTo(-6);
        make.width.height.mas_equalTo(24);
    }];
    
    
    UIImageSymbolConfiguration *config = [UIImageSymbolConfiguration configurationWithPointSize:30 weight:UIImageSymbolWeightRegular];
    UIImage *image = [UIImage systemImageNamed:@"play.circle" withConfiguration:config];
    self.videoImageView = [[UIImageView alloc] initWithImage:image];
    self.videoImageView.hidden = YES;
    [self.contentView addSubview:self.videoImageView];
    [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
}
- (UIView *)liveTagView {
    if (_liveTagView == nil) {
        _liveTagView = [[UIView alloc] init];
        _liveTagView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        _liveTagView.layer.cornerRadius = 4;
        _liveTagView.layer.masksToBounds = YES;

        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"livephoto"]];
        iconView.contentMode = UIViewContentModeScaleAspectFit;
        [_liveTagView addSubview:iconView];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"LIVE";
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:11];
        [_liveTagView addSubview:label];

      
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(6);
            make.centerY.equalTo(_liveTagView);
            make.width.height.mas_equalTo(16);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconView.mas_right).offset(4);
            make.right.mas_equalTo(-6);
            make.centerY.equalTo(_liveTagView);
        }];
    }
    return _liveTagView;
}
@end
